#include "udispbookwidget.h"
#include "udispbookinfobox.h"
#include "uscrollbar.h"
#include <QScrollArea>
UDispBookWidget::UDispBookWidget(QWidget *parent)
	: QWidget(parent)
	, m_pScrollBar(new UScrollBar(this))
{
	setWindowFlags(Qt::FramelessWindowHint);
	setAttribute(Qt::WA_StyledBackground, true);//默认Qwidget类 无法设置颜色
	setFixedSize(650, 400);
	setStyleSheet(R"(UDispBookWidget{background-color:#FFFFFF;})");
	connect(UClientServer::Instance(), &UClientServer::alreadyRead, this, &UDispBookWidget::readBooksInfoList);
	connect(m_pScrollBar, &UScrollBar::valueChanged, this, &UDispBookWidget::onScrollBarValueChanged);
	m_pScrollBar->move(width() - 8, 0);
}

void UDispBookWidget::onScrollBarValueChanged(int value)
{
	int sh = -(m_pBoxList.size() * 127 - height()) * value / 100.;
	for (int i = 0; i < m_pBoxList.size(); ++i)
		m_pBoxList[i]->move(0, sh + i * 127);
}

void UDispBookWidget::readBooksInfoList(const OPERATOR type, QByteArray datagram)
{
	clearWidget();
	if (type == OPERATOR::QUERYBOOK)
	{
		QDataStream ds(datagram);
		OPERATOR buffer;
		QString list;
		ds >> buffer >> list;

		Json::CharReaderBuilder reader;
		Json::Value parsedBooksArray;
		std::string errs;
		std::istringstream s(list.toStdString());
		bool parsingSuccessful = Json::parseFromStream(reader, s, &parsedBooksArray, &errs);

		if (!parsingSuccessful) {
			std::cerr << "解析失败： " << errs << std::endl;
			return;
		}
		if (!parsedBooksArray.isArray()) {
			std::cerr << "JSON字段不是数组：" << std::endl;
			return;
		}

		for (const auto& bookJson : parsedBooksArray) {
			UBooksInfo book;
			book.m_isbn = QString::fromStdString(bookJson["isbn"].asString());
			book.m_title = QString::fromStdString(bookJson["title"].asString());
			book.m_author = QString::fromStdString(bookJson["author"].asString());
			book.m_press = QString::fromStdString(bookJson["press"].asString());
			book.m_date = QString::fromStdString(bookJson["date"].asString());
			book.m_image = QString::fromStdString(bookJson["image"].asString());
			addContactBox(book);
		}

		if (parsedBooksArray.size() > 3)
			m_pScrollBar->show();
		else
			m_pScrollBar->hide();

		for (int i = 0; i < m_pBoxList.size(); ++i)
		{
			m_pBoxList[i]->move(0, i * 127);
			m_pBoxList[i]->show();
		}
	}
	update();
}

void UDispBookWidget::clearWidget()
{
	if (!m_pBoxList.isEmpty())
	{
		for (int i = 0; i < m_pBoxList.size(); ++i)
		{
			m_pBoxList[i]->close();
			delete m_pBoxList[i];
			m_pBoxList[i] = nullptr;		//清空时直接删除并置空对象，优化性能
		}
		m_pBoxList.clear();
	}

	if (!m_pBooksInfoList.empty())  //T为非指针类型，直接clear即可。指针类型需要qDeleteAll(m_pBooksInfoList)
		m_pBooksInfoList.clear();
}

void UDispBookWidget::addContactBox(UBooksInfo book)
{
	UDispBookInfoBox* bookInfoBox = new UDispBookInfoBox(this, book.m_title, book.m_author, book.m_press,
															   book.m_isbn, book.m_date, book.m_image);
	m_pBoxList.push_back(bookInfoBox);
	m_pBooksInfoList.push_back(book);
}

QDataStream& operator<<(QDataStream& out, const UBooksInfo& books)
{
	return out << books.m_isbn
		<< books.m_title
		<< books.m_author
		<< books.m_press
		<< books.m_date
		<< books.m_image;
}

QDataStream& operator>>(QDataStream& in, UBooksInfo& books)
{
	return in >> books.m_isbn
		>> books.m_title
		>> books.m_author
		>> books.m_press
		>> books.m_date
		>> books.m_image;
}


UDispBookWidget::~UDispBookWidget()
{}
